package com.kakarote.authorization.service.impl;

import cn.hutool.core.util.StrUtil;
import com.kakarote.authorization.common.LoginType;
import com.kakarote.authorization.entity.AuthenticatedUser;
import com.kakarote.authorization.entity.AuthorizationUser;
import com.kakarote.authorization.service.AdminUserService;
import com.kakarote.authorization.service.UserAuthenticationService;
import com.kakarote.core.feign.admin.entity.AdminUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 飞书扫码和飞书免登录
 *
 * @author zhangzhiwei
 */
@Service
public class FlyUserAuthenticationService implements UserAuthenticationService {

    @Autowired
    private AdminUserService adminUserService;

    /**
     * 用户查找
     *
     * @param authorizationUser 认证信息
     * @return 已认证用户
     */
    @Override
    public AuthenticatedUser loadUser(AuthorizationUser authorizationUser) {
        List<AdminUserInfo> infoList = adminUserService.findUserByFlyCode(authorizationUser.getCpCode(), authorizationUser.getCorpId()).getData();
        AuthenticatedUser authenticatedUser = new AuthenticatedUser();
        authenticatedUser.setUserInfoList(infoList);
        return authenticatedUser;
    }

    /**
     * 是否支持该类型的认证
     *
     * @param loginType 认证类型
     * @return 是否支持
     */
    @Override
    public boolean supports(LoginType loginType) {
        return LoginType.FLY_CODE.equals(loginType) || LoginType.FLY_QR_CODE.equals(loginType);
    }

    /**
     * 是否是正常的用户
     *
     * @param userInfo  用户信息
     * @param extraData 额外数据
     * @return 用户是否正常，true为正常
     */
    @Override
    public boolean isNormalUser(AdminUserInfo userInfo, Object extraData) {
        return true;
    }

    /**
     * 验证参数是否正确
     *
     * @param authorizationUser 当前登录用户信息
     * @return true为正常
     */
    @Override
    public boolean verifyParameter(AuthorizationUser authorizationUser) {
        return StrUtil.isNotEmpty(authorizationUser.getCpCode()) && StrUtil.isNotEmpty(authorizationUser.getCorpId());
    }
}
